#!/bin/bash
#Date 2022/1/18
#mail it-arch

[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
which  ntpdate
if [ $? -eq 0 ];then
	/usr/sbin/ntpdate time1.aliyun.com
	echo "*/5 * * * * /usr/sbin/ntpdate -s time1.aliyun.com">>/var/spool/cron/root	
else
	yum install ntpdate -y
	/usr/sbin/ntpdate time1.aliyun.com
	echo "*/5 * * * * /usr/sbin/ntpdate -s time1.aliyun.com">>/var/spool/cron/root	
fi
yum remove `yum list installed | grep php | awk '{print $1}'` -y
clear
if [ -n "`grep 'CentOS Linux release 7' /etc/redhat-release`" ];then
        CentOS_REL=el7
        rpm -ivh http://repo.webtatic.com/yum/el7/epel-release.rpm
		ls /etc/yum.repos.d/webtatic.repo
		if [ $? -eq 0 ];then
			sed -i "s/#baseurl=/baseurl=/g" /etc/yum.repos.d/webtatic.repo
			sed -i "s/mirrorlist=/#mirrorlist=/g" /etc/yum.repos.d/webtatic.repo
		fi
elif [ -n "`grep 'CentOS release 6' /etc/redhat-release`" ];then
        CentOS_REL=6
        rpm -ivh http://repo.webtatic.com/yum/el6/latest.rpm
	if [ $? -eq 0 ];then
		sed -i "s/#baseurl=/baseurl=/g" /etc/yum.repos.d/webtatic.repo
		sed -i "s/mirrorlist=/#mirrorlist=/g" /etc/yum.repos.d/webtatic.repo
	fi
elif [ -n "`grep 'CentOS release 5' /etc/redhat-release`" ];then
        CentOS_REL=5
        rpm -ivh http://repo.webtatic.com/yum/el5/latest.rpm
	if [ $? -eq 0 ];then
		sed -i "s/#baseurl=/baseurl=/g" /etc/yum.repos.d/webtatic.repo
		sed -i "s/mirrorlist=/#mirrorlist=/g" /etc/yum.repos.d/webtatic.repo
	fi
else
        echo -e "\033[31mDoes not support this OS, Please contact the author! \033[0m"
        exit 1
fi
clear
echo "##########################################"
echo "#					                       #"
echo "#   Auto 编译安装 PHP5.5/5.6/7.0/7.1.    #"
echo "#					                       #"
echo "##########################################"
echo "1 Install PHP-5.5"
echo "2 Install PHP-5.6"
echo "3 Install PHP-7.0"
echo "4 Install PHP-7.1"
echo "5 EXIT"
read -p "Please input your choice:" I
if [ "$I" == "1" ];then
	VER="php55w"
elif [ "$I" == "2" ];then
	VER="php56w"
elif [ "$I" == "3" ];then
	VER="php70w"
elif [ "$I" == "4" ];then
	VER="php71w"
elif [ "$I" == "5" ];then
        echo "you choce channel!"
        exit 1;
else
        echo "input Error! Place input{1|2|3|4|5}"
        exit 0;
fi
clear
echo -e "\033[32m You choice install $VER.Install\033[0m"
echo -e "\033[32m Press any key to start install $VER... \033[0m"
read -n 1
ls /etc/yum.repos.d/webtatic.repo
if [ $? -eq 0 ];then
		yum -y install `yum list|grep php71w|awk '{printf ("%s ",$1)}'` --skip-broken
fi
chkconfig php-fpm on
service php-fpm start /tmp/php-install.log 2>&1
STAT=`echo $?`
PORT=`netstat -lntup|grep php-fpm|wc -l`
if [ $STAT -eq 0 ] && [ $PORT -eq 1 ];then
	echo -e "\033[32m PHP is install success! \033[0m"
else
	echo -e "\033[32m PHP install file.please check /tmp/php-install.log \033[0m"
fi
